﻿<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<!--
  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.

  Use of this source code is governed by a BSD-style license
  that can be found in the LICENSE file in the root of the source
  tree. An additional intellectual property rights grant can be found
  in the file PATENTS.  All contributing project authors may
  be found in the AUTHORS file in the root of the source tree.
-->
<html>
<head>
  <title>WebRTC PeerConnection Fuzz Test Template</title>
INCLUDE_RANDOM_JS
INCLUDE_FUZZ_SDP_JS
  <script type="text/javascript">
  var gFirstConnection = null;
  var gSecondConnection = null;

  // Variables in caps are filled in by the fuzzer.
  var gTransformOfferSdp = TRANSFORM_OFFER_SDP;
  var gTransformAnswerSdp = TRANSFORM_ANSWER_SDP;

  // START_OF_POSSIBLE_INJECTED_LOCATION_RELOADS
  function startTest() {
    navigator.webkitGetUserMedia(REQUEST_AUDIO_AND_VIDEO,
                                 getUserMediaOkCallback,
                                 getUserMediaFailedCallback);
  }

  function getUserMediaFailedCallback(error) {
    console.log('getUserMedia request failed with code ' + error.code);
  }

  function callUsingStream(localStream) {
    gFirstConnection = new webkitPeerConnection00(
        null, onIceCandidateToFirst);
    gFirstConnection.addStream(localStream);
    var offer = gFirstConnection.createOffer(null);
    gFirstConnection.setLocalDescription(
        webkitPeerConnection00.SDP_OFFER, offer);
    return offer.toSdp();
  }

  function receiveCall(offerSdp) {
    gSecondConnection = new webkitPeerConnection00(
        null, onIceCandidateToSecond);
    gSecondConnection.onaddstream = onRemoteStream;
    var parsedOffer = new SessionDescription(offerSdp);
    gSecondConnection.setRemoteDescription(
        webkitPeerConnection00.SDP_OFFER, parsedOffer);

    var answer = gSecondConnection.createAnswer(
        offerSdp, { has_audio: true, has_video: true });
    gSecondConnection.setLocalDescription(
        webkitPeerConnection00.SDP_ANSWER, answer);
    gSecondConnection.startIce();
    return answer.toSdp();
  }

  function handleAnswer(answerSdp) {
    var parsed_answer = new SessionDescription(answerSdp);
    gFirstConnection.setRemoteDescription(
        webkitPeerConnection00.SDP_ANSWER, parsed_answer);
    gFirstConnection.startIce();
  }

  function getUserMediaOkCallback(localStream) {
    var localStreamUrl = webkitURL.createObjectURL(localStream);
    document.getElementById('local-view').src = localStreamUrl;

    var offerSdp = callUsingStream(localStream);
    offerSdp = gTransformOfferSdp(offerSdp);
    var answerSdp = receiveCall(offerSdp);
    answerSdp = gTransformAnswerSdp(answerSdp);
    console.log(offerSdp);
    console.log(answerSdp);
    handleAnswer(answerSdp);
  }

  function onIceCandidateToFirst(candidate, more) {
    if (candidate) {
      gSecondConnection.processIceMessage(candidate);
    }
  }

  function onIceCandidateToSecond(candidate, more) {
    if (candidate) {
      gFirstConnection.processIceMessage(candidate);
    }
  }

  function onRemoteStream(e) {
    var remoteStreamUrl = webkitURL.createObjectURL(e.stream);
    document.getElementById('remote-view').src = remoteStreamUrl;
  }

  window.onload = function() {
    setRandomRolls(gRandomRolls);
    startTest();
  }
  // END_OF_POSSIBLE_INJECTED_LOCATION_RELOADS

  // This variable is placed here since its value is pretty big.
  var gRandomRolls = ARRAY_OF_RANDOM_ROLLS;
  </script>
</head>
<body>
  <table border="0">
    <tr>
      <td>Local Preview</td>
      <td>Remote Stream</td>
    </tr>
    <tr>
      <td><video width="320" height="240" id="local-view"
          autoplay="autoplay"></video></td>
      <td><video width="320" height="240" id="remote-view"
          autoplay="autoplay"></video></td>
    </tr>
  </table>
</body>
</html>